//
//  WJQForgotpassViewController.m
//  TripTogether
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 杨泓晔. All rights reserved.
//

#import "WJQForgotpassViewController.h"
#import <BmobSDK/Bmob.h>
#import <SMS_SDK/SMSSDK.h>


@interface WJQForgotpassViewController ()

@property (nonatomic, strong) UITextField *phoneTextF;
@property (nonatomic, strong) UITextField *codeTextF;
@property (nonatomic, strong) UIButton *codeButton;
@property (nonatomic, strong) UITextField *passTextF;
@property (nonatomic, strong) UITextField *nPassTextF;

@property (nonatomic, strong) UIButton *registerButton;

@end

@implementation WJQForgotpassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:0.320 green:0.740 blue:0.800 alpha:1.000];
    
    [self createBackButton];
    
     [self createSubViews];
}
- (void)createSubViews
{
    self.view.backgroundColor = [UIColor colorWithRed:0.320 green:0.740 blue:0.800 alpha:1.000];
    
    
    UILabel *zoneL = [UILabel new];
    //    zoneL.backgroundColor = [UIColor orangeColor];
    zoneL.text = @"+86";
    zoneL.textAlignment = NSTextAlignmentCenter;
    zoneL.textColor = [UIColor whiteColor];
    [self.view addSubview:zoneL];
    
    [zoneL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(50, 30));
        make.top.mas_equalTo(150 * AUTO_HEIGHT);
        make.left.mas_equalTo(30 * AUTO_WIDTH);
    }];
    
    _phoneTextF = [UITextField new];
    _phoneTextF.backgroundColor = [UIColor colorWithRed:0.320 green:0.740 blue:0.800 alpha:1.000];
    //    _phoneTextF.borderStyle = UITextBorderStyleBezel;
    _phoneTextF.placeholder = @"请输入手机号码";
    _phoneTextF.font = [UIFont fontWithName:@"Lao Sangam MN" size:16];
    _phoneTextF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [self.view addSubview:_phoneTextF];
    _phoneTextF.textColor = [UIColor whiteColor];
    
    [_phoneTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(zoneL);
        make.left.mas_equalTo(zoneL.mas_right).offset(20 * AUTO_WIDTH);
        make.right.mas_equalTo(-20 * AUTO_WIDTH);
        make.height.mas_equalTo(zoneL);
    }];
    
    /* 改变placeholder的颜色 */
    [_phoneTextF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    
    
    
    UIView *lin = [UIView new];
    lin.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:lin];
    
    [lin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).offset(20 * AUTO_WIDTH);
        make.top.mas_equalTo(_phoneTextF.mas_bottom).offset(15 * AUTO_HEIGHT);
        make.right.mas_equalTo(self.view).offset(- 20 * AUTO_WIDTH);
        make.height.mas_equalTo(@1);
        
    }];
    
    
    
    
    
    
    
    _codeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _codeButton.backgroundColor = [UIColor whiteColor];
    [_codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_codeButton setTitleColor:self.view.backgroundColor forState:UIControlStateNormal];
    _codeButton.titleLabel.font = [UIFont systemFontOfSize:13.f];
    _codeButton.layer.cornerRadius = 12.f;
    
    [_codeButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_codeButton];
    
    
    
    
    
    _codeTextF = [UITextField new];
    // _codeTextF.borderStyle = UITextBorderStyleRoundedRect;
    _codeTextF.placeholder = @"短信验证码";
    [_codeTextF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    _codeTextF.font = [UIFont fontWithName:@"Lao Sangam MN" size:16];
    _codeTextF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [self.view addSubview:_codeTextF];
    
    
    
    [_codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(80, 26));
        make.right.mas_equalTo(_phoneTextF);
        make.top.mas_equalTo(lin.mas_top).offset(20 * AUTO_HEIGHT);
        
    }];
    _codeTextF.textColor = [UIColor whiteColor];
    
    [_codeTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(lin.mas_bottom).offset(20 * AUTO_HEIGHT);
        make.left.mas_equalTo(zoneL.mas_left);
        make.right.mas_equalTo(_codeButton.mas_left).offset(-20 * AUTO_WIDTH) ;
        make.height.mas_equalTo(_phoneTextF.mas_height);
        
        
    }];
    
    
    
    UIView *viewlin = [UIView new];
    viewlin.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewlin];
    [viewlin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_codeTextF.mas_bottom).offset(15 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.view).offset(20  *AUTO_WIDTH);
        make.right.mas_equalTo(self.view).offset(- 20 * AUTO_WIDTH);
        make.height.mas_equalTo(@1);
    }];
    
    
    
    
    
    
    
    
    
    _passTextF = [UITextField new];
    
    //  _passTextF.borderStyle = UITextBorderStyleRoundedRect;
    _passTextF.placeholder = @"输入旧密码";
    _passTextF.secureTextEntry = YES;
    [self.view addSubview:_passTextF];
    [_passTextF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    _passTextF.font = [UIFont fontWithName:@"Lao Sangam MN" size:16];
    _passTextF.textColor = [UIColor whiteColor];
    
    [_passTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_codeTextF.mas_bottom).offset(30 * AUTO_HEIGHT);
        make.height.mas_equalTo(_codeTextF);
        make.right.mas_equalTo(_codeButton.mas_right);
        make.left.mas_equalTo(_codeTextF);
        
    }];

    
    UIView *viewl = [UIView new];
    viewl.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewl];
    [viewl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_passTextF.mas_bottom).offset(15 *AUTO_HEIGHT);
        make.left.mas_equalTo(self.view).offset(20 *AUTO_WIDTH);
        make.right.mas_equalTo(self.view).offset(- 20 * AUTO_WIDTH);
        make.height.mas_equalTo(@1);
        
        
    }];
    
    
    _nPassTextF = [UITextField new];
    _nPassTextF.placeholder = @"输入新密码";
    _nPassTextF.secureTextEntry = YES;
    [_nPassTextF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    _nPassTextF.font = [UIFont fontWithName:@"Lao Sangam MN" size:16];
    _nPassTextF.textColor = [UIColor whiteColor];
    [self.view addSubview:_nPassTextF];
    
    [_nPassTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_passTextF.mas_left);
        make.height.mas_equalTo(_passTextF.mas_height);
        make.top.mas_equalTo(_passTextF.mas_bottom).offset(30 * AUTO_HEIGHT);
        make.right.mas_equalTo(_passTextF.mas_right);
    }];

    UIView *nPasslin = [UIView new];
    nPasslin.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:nPasslin];
    [nPasslin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nPassTextF.mas_bottom).offset(15 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.view).offset(20  *AUTO_WIDTH);
        make.right.mas_equalTo(self.view).offset(- 20 * AUTO_WIDTH);
        make.height.mas_equalTo(@1);
    }];
    
    
    
    _registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _registerButton.backgroundColor = [UIColor clearColor];
    [_registerButton setTitle:@"同意协议并注册" forState:UIControlStateNormal];
    [_registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _registerButton.layer.borderWidth = 1.5f;
    _registerButton.layer.borderColor = [UIColor whiteColor].CGColor;
    _registerButton.layer.cornerRadius = 21.f;
    [_registerButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registerButton];
    
    [_registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(200, 40));
        make.centerX.mas_equalTo(@0);
        make.top.mas_equalTo(_passTextF.mas_bottom).offset(100 * AUTO_HEIGHT);
        
        
    }];
  
    
    
    
    

    
    
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.view addGestureRecognizer:tap];
    
    //监听键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
}
- (void)tapAction
{
    if ([_codeTextF isFirstResponder]) {
        [_codeTextF resignFirstResponder];
    }
    if ([_passTextF isFirstResponder]) {
        [_passTextF resignFirstResponder];
    }
    if ([_phoneTextF isFirstResponder]) {
        [_phoneTextF resignFirstResponder];
    }
    if ([_nPassTextF isFirstResponder]) {
        [_nPassTextF resignFirstResponder];
    }
}
- (void)keyboardWillChangeFrameNotification:(NSNotification *)notification{
    
    //取出键盘动画的时间(根据userInfo的key----UIKeyboardAnimationDurationUserInfoKey)
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    //取到键盘的frame
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    //计算控制器的view需要平移的距离
    CGFloat transformY = ([[UIScreen mainScreen] bounds].size.height - 200 * AUTO_HEIGHT) - keyboardFrame.origin.y;
    
    // 修改下边距约束

//    NSLog(@"%f", transformY);
//    [UIView animateWithDuration:duration animations:^{
//        
//        CGRect rect = self.view.frame;
//        rect.origin.y = -transformY;
//        self.view.frame = rect;
//    }];
    // 更新约束gvdfgf
//    [UIView animateWithDuration:duration animations:^{
//        [self.view layoutIfNeeded];
//    }];
    
}

- (void)buttonAction:(UIButton *)sender
{
    if (_codeButton == sender) {
        
        if ([self checkTel:_phoneTextF.text]) {
            // 手机格式验证成功
            
            _codeButton.userInteractionEnabled = NO;
            [_codeButton setTitle:@"正在获取中" forState:UIControlStateNormal];
            
            // 查找用户
            BmobQuery *query = [BmobUser query];
            [query whereKey:@"mobilePhoneNumber" equalTo:_phoneTextF.text];
            
            [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
                
                if (array.count > 0) {
                    // 手机已经注册过了
                    [self alertControllerWithTitle:@"提示" message:@"该手机已注册"];
                    
                    _codeButton.userInteractionEnabled = YES;
                    [_codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                    
                    return;
                } else {
                    
                    [self getcode];
                }
                for (BmobUser *user in array) {
                    NSLog(@"objectid %@",user.username);
                }
            }];
            
            
            
        }
        
    } else if (_registerButton == sender) {
        // 点击注册按钮
        
        
    } else {
        [self alertControllerWithTitle:@"密码格式不对 请重新输入" message:@"密码只能给你包含数字和字母, 至少6位最长20位"];
    }
}
-(void)getcode
{
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_phoneTextF.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        
        if (!error) {
            
            NSLog(@"获取验证码成功");
            
        } else {
            
            
        }
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
